﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using FictionsDownload.Model;

namespace FictionsDownload.BLL
{
    /// <summary>
    /// Html相关的静态方法
    /// </summary>
    public class HtmlHelper
    {
        /// <summary>
        /// 字符串：从指定的开头截取到指定的结尾（不含查找的开头结尾）
        /// </summary>
        /// <param name="str"></param>
        /// <param name="left"></param>
        /// <param name="right"></param>
        /// <returns></returns>
        public static string HtmlTrim(string str, string findLeft = null, string findRight = null)
        {
            int htmlLeft = 0;
            int htmlRight = 0;

            if (findLeft == null && findRight == null)
            {
                return str;
            }
            else if(str == null)
            {
                return str;
            }

            //字符串起点判断
            if (findLeft != null)
            {   //前过滤有值，判断Left的位置
                htmlLeft = str.IndexOf(findLeft);
            }
            if(htmlLeft < 0)
            {
                htmlLeft = 0;
            }
            else
            {
                htmlLeft += findLeft == null ? 0:findLeft.Length;
            }

            //字符串长度判断
            if(findRight == null)
            {
                htmlRight = 0;
            }
            else if(findRight != null)
            {
                htmlRight = str.IndexOf(findRight);
            }
            if (htmlRight < 0)
            {
                htmlRight = 0;
            }


            if (htmlLeft == 0 && htmlRight == 0)
            {
                return str;
            }
            else if(htmlRight == 0)
            {
                return str.Substring(htmlLeft);
            }
            else if(htmlRight <= htmlLeft)
            {
                return str;
            }
            else
            {
                return str.Substring(htmlLeft, htmlRight - htmlLeft);
            }
        }
    }
}
